<?php
/*

  Copyright (c) 2008   by  maxim.oransky <maxim.oransky@gmail.com>,
  auhtor of  the-wheel-library  team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class File {

	public static function load($fname) {
		self::checkExistsAndReadable($fname);
		return file_get_contents($fname);
	}

	public static function isExistsOnSite($url) {
		return file_exists(Dir::getRoot() . $url);
	}

	public static function save($fname, $str) {

		if (file_exists($fname)) self::checkReadable($fname);

		$f = fopen($fname, 'w');
		flock($f, LOCK_EX);
		fwrite($f, $str);
		flock($f, LOCK_UN);
		fclose($f);
	}

	public static function append($fname,$str) {

		if (file_exists($fname)) self::checkReadable($fname);

		$f = fopen($fname, 'a');
		flock($f, LOCK_EX);
		fwrite($f, $str);
		flock($f, LOCK_UN);
		fclose($f);
	}

	public static function delete($fname) {
		@unlink($fname);
		if (file_exists($fname)) throw new Exception("can't delete [$fname]");
	}

	public static function getUrl($fname)	{

		if (is_array($fname)) return array_map(array('File','getUrl'),$fname);

		if (file_exists($fname)) $fname = realpath($fname);
		$fname = substr($fname, strlen(Dir::getRoot()));
		return Dir::unixSlashes($fname);
	}

	public static function getFilenameHash($fname) {
		return strtr($fname,array(
			' '  => '_',
			'/'  => '--',
			'\\' => '--',
			'.'  => '-',
		));
	}

	public static function checkReadable($fname) {

		if (!is_readable($fname))
			throw new Exception("[$fname] is not readable");

		if (!is_file($fname))
			throw new Exception("[$fname] isn't file");
	}

	public static function checkExistsAndReadable($fname) {

		if (!file_exists($fname))
			throw new Exception("[$fname] isn't exists");

		self::checkReadable($fname);
	}

	public static function getHumanSize($path) {
		return HumanFormat::filesize(filesize($path));
	}

	public static function getMp3Info($fname) {

		$f = fopen($fname, 'rb');
		fseek($f, -128, SEEK_END);
		$s = fread($f, 128);
		fclose($f);

		return (substr($s,0, 3) == 'TAG') ?

			array(
				'title'  	=> trim(substr($s,3,30)),
				'artist' 	=> trim(substr($s,33,30)),
				'album'  	=> trim(substr($s,63,30)),
				'year'   	=> trim(substr($s,93,4)),
				'comment'	=> trim(substr($s,97,30)),
			):

			array(
				'title'  	=> basename($fname),
				'artist' 	=> '',
				'album'  	=> '',
				'year'   	=> '',
				'comment'	=> '',
			);
	}

	public static function safeFilename($filename) {
	    return strtr($filename, array(
	       ' ' => '_',
	       ':' => '-',
	       '/' => '',
	       '\\' => '',
	    ));
	}
}

?>